
package com.jml.mayi.consumer;

import com.jml.utils.HttpClientUtils;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
import java.util.Map;

import org.springframework.amqp.core.Message;
import org.springframework.messaging.handler.annotation.Headers;

import com.alibaba.fastjson.JSONObject;
import com.rabbitmq.client.Channel;

@Component
public class FanoutEamilConsumer全局唯一ID {

	@RabbitListener(queues = "fanout_eamil_queue_id")
	public void process(Message message, @Headers Map<String, Object> headers, Channel channel) throws Exception {
		//得到全局ID
		String messageId = message.getMessageProperties().getMessageId();
		//得到消息
		String msg = new String(message.getBody(), "UTF-8");
		System.out.println("邮件消费者获取生产者消息msg:" + msg + ",消息id:" + messageId);
		// 重试机制都是间隔性
		JSONObject jsonObject = JSONObject.parseObject(msg);
		String email = jsonObject.getString("email");
		String emailUrl = "http://127.0.0.1:8083/sendEmail?email=" + email;
		System.out.println("邮件消费者开始调用第三方邮件服务器,emailUrl:" + emailUrl);
		JSONObject result = HttpClientUtils.httpGet(emailUrl);
		// 如果调用第三方邮件接口无法访问，如何实现自动重试.
		if (result == null) {
			throw new Exception("调用第三方邮件服务器接口失败!");
		}
		System.out.println("邮件消费者结束调用第三方邮件服务器成功,result:" + result + "程序执行结束");

	}
}

